{{include file="public/header" /}}

<!-- nav -->
{{include file="public/nav" /}}
		
<!-- content -->
<div class="admin">
	<!-- left menu -->
	{{include file="public/menu" /}}

	<!-- right content  -->
	<iframe id="ifcontent" src="{{$to_url}}" width="100%" height="100%"></iframe>
	<!-- right content end  -->



</div>

<!-- navbar -->
{{include file="public/nav_bar" /}}
		
<!-- footer -->
{{include file="public/footer" /}}



<script type="text/javascript">
	function transformLanguageMenu(newLanguage) {
		// 获取所有dom元素中文
		var transformStr = '';
		var temp_index = 0;
		// 获取所有dom元素
		function getChildDom(dom, type, data = {}) {
			if(type == 'read') {
				[...dom.children].forEach(v => {
					// 判断中文
					// /^[\u0391-\uFFE5]+$/
					var re= /[\u4e00-\u9fa5]/g;
					// 防止某些标签有内容并且有标签 ，或者有空格
					var vHtml = $(v).contents().filter(function (index, content) {return content.nodeType === 3}).text().trim();
					// 跳过script标签
					if (re.test(vHtml) && v.tagName != 'SCRIPT') {
						// 使用\n换行拼接、接口将返回数组
						transformStr += `${vHtml}\n`
					}
					// 递归获取元素
					getChildDom(v, type, data);
				})
			}else {
				[...dom.children].forEach(v => {
					// 判断中文
					// /^[\u0391-\uFFE5]+$/
					var re= /[\u4e00-\u9fa5]/g;
					var vHtml = $(v).contents().filter(function (index, content) {return content.nodeType === 3}).text().trim();
					// 跳过script标签
					if (re.test(vHtml) && v.tagName != 'SCRIPT') {
						if((data[temp_index] || null) != null)
						{
							var transOld = data[temp_index]['src'];
							var transNew = data[temp_index]['dst'];
							// 防止标签里面还有标签，所以只替换里面的html,使用replace
							$(v).html(
									$(v).html().replace( transOld, transNew)
							)
							temp_index++;
						}
					}
					// 递归获取元素
					getChildDom(v, type, data);
				})
			}
		}
		getChildDom(document,'read');
		if((transformStr || null) != null)
		{
			getTranslateData();
			// 获取翻译
			// 接口申请地址 https://api.fanyi.baidu.com/
			function getTranslateData() {
				var appid = '20210425000799675';   // 百度翻译API的appid
				var key = 'VNlqhTcdY1QU09HfHN5L';   // 百度翻译API的key
				var salt = (new Date).getTime();
				var query = transformStr;
				var from = 'zh';
				var to = newLanguage;
				if(newLanguage=="zh"){
					to = newLanguage;
					from = 'spa';
				}
				var str1 = appid + query + salt + key;
				var sign = md5(str1);
				$.ajax({
					url: 'api/index/tranlange',
					type: 'post',
					dataType: 'json',
					data: {
						q: query,
						appid: appid,
						salt: salt,
						from: from,
						to: to,
						sign: sign
					},
					success: function(data) {
						data.trans_result && getChildDom(document,'write',data.trans_result);
					}
				});
			}
		}
	}

	// 页面加载完成再执行
	window.onload = function () {
		if(localStorage.getItem("language")==undefined||localStorage.getItem("language")==null){
			setTimeout(function(){transformLanguageMenu('spa');},3000)
			localStorage.setItem("language","spa");
		}else if (localStorage.getItem("language")=="spa"){
			setTimeout(function(){transformLanguageMenu('spa');},3000)
			localStorage.setItem("language","spa");
		}

		// transformLanguage1('en');
		// $("#china").click(function(){
		//     console.log(1);
		//     transformLanguage('zh');
		// })
		// $("#english").click(function(){
		//     console.log(2);
		//     transformLanguage('en');
		// })
	};
</script>
